#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""The default <IEmbeddedResolver> implementation."""

namespace Grawlix.Services.Resources
import Grawlix.Properties
import Grawlix.Services
import System

transient class DefaultEmbeddedResolver(ServiceBase, IEmbeddedResolver):
"""The default service that resolves URIs into embedded resources."""
	
	def constructor(uriResolver as IUriResolver):
	"""
	Initializes a new embedded resolver.
	Param uriResolver: The URI translator.
	"""
		_uriResolver = uriResolver
	
	def Load(uri as Uri, allowLocal as bool) as object:
	"""
	Load an embedded assembly resource by its URI.
	Param uri: The URI which identifies the embedded resource.
	Param allowLocal: Whether to allow local URI schemes,
		such as file:// and internal://.
	Returns: The embedded resource found.
	Raises IOException: A generic I/O error occurred.
	Raises FileNotFoundException:
		The URI could not be resolved or
		it does not point to an embedded resource.
	Remarks: Note that this is a blocking operation which should only be
		called asynchronously to avoid making the game hang. Reading from
		the stream is also a blocking operation.
	"""
		embedded = TryLoad(uri, allowLocal)
		return embedded unless embedded is null
		raise IO.FileNotFoundException(
			"A request was made to resolve a universal resource identifier " +
			"(${uri}) to an embedded assembly resource, but " +
			"no such resource was found. " +
			"The resource requested was not necessarily a critical " +
			"component, but it will not be accessible.")
	
	def TryLoad(uri as Uri, allowLocal as bool) as object:
	"""
	Load an embedded assembly resource by its URI.
	Param uri: The URI which identifies the embedded resource.
	Param allowLocal: Whether to allow local URI schemes,
		such as file:// and internal://.
	Returns: The embedded resource found, or null if not found or invalid.
	Raises IOException: A generic I/O error occurred.
	Remarks: Note that this is a blocking operation which should only be
		called asynchronously to avoid making the game hang. Reading from
		the stream is also a blocking operation.
	"""
		resolved = _uriResolver.TryResolve(uri, allowLocal)
		return null if resolved is null
		if resolved.Scheme == DefaultUriResolver.InternalScheme:
			return BuiltIn.ResourceManager.GetObject(
				uri.AbsolutePath[1:])
		return null
	
	override def ToString():
	"""
	Get a string representation for this object.
	Returns: A string representation for this object.
	"""
		return "DefaultEmbeddedResolver"
	
	private _uriResolver as IUriResolver
	"""The URI translation service."""
